const express = require("express");
const mongoose = require("mongoose");
const bp = require("body-parser");
const fs = require("fs");
const fd = require("formidable");

const app = express();
app.use(bp.urlencoded({extended:false}));
app.use(express.static("./"));

app.listen(1818,function () {
    mongoose.connect("mongodb://localhost/nodes",{
        useNewUrlParser:true,
        useUnifiedTopology:true
    });
})

mongoose.connection.on("open",function () {
    console.log("数据库连接成功")
    const schemaObj = new mongoose.Schema({
        name: String,
        pwd: String,
        date: String,
    }, {collection: "user", versionKey: false})

    const myModel = mongoose.model("user", schemaObj);

    app.get("/getUser", function (req, res) {
        myModel.find(function (error, result) {
            if (!error) {
                res.json(result);
            } else {
                res.json(error);
            }
        })
    })

    app.post("/manager", function (req, res) {
        // let name = req.body.username;
        // let pwd = req.body.password;
        console.log(req.body)
        myModel.create({
            name: req.body.name,
            pwd: req.body.pwd,
            date: req.body.date
        }, function (error, result) {
            if (!error) {
                res.json(result);
            } else {
                res.json(error);
            }
        })
    })


    //店铺
    const schemaObj2 = new mongoose.Schema({
        storename: String,
        storeaddress: String,
        storeNumber: String,
        startmoney: Number,
        litmoney: Number,
        pictures: String
    }, {collection: "store", versionKey: false})

    const myModel2 = mongoose.model("store", schemaObj2);


    let imgu;
    app.post("/uploadF", function (req, res) {
        let uploadObj = new fd.IncomingForm();
        uploadObj.parse(req, function (error, fields, files) {
            let fileInfo = files.my_file;
            let file_name = fileInfo.name;
            let file_temp_path = fileInfo.path;
            let target_path = "store/" + file_name;
            let readS = fs.createReadStream(file_temp_path);

            readS.on("end", function () {
                res.json({msg: "ok", img_src: target_path});
            });
            readS.on("error", function () {
                res.json({msg: "error"});
            });
            let writeS = fs.createWriteStream(target_path);
            imgu = target_path;
            readS.pipe(writeS);
        });
    });


    app.get("/getStore", function (req, res) {
        myModel2.find(function (error, result) {
            if (!error) {
                res.json(result);
            } else {
                res.json(error);
            }
        })
    })

    app.post("/store", function (req, res) {
        myModel2.create({
            storename: req.body.storename,
            storeaddress: req.body.storeaddress,
            storeNumber: req.body.storeNumber,
            startmoney: req.body.startmoney,
            litmoney: req.body.litmoney,
            storeimg: imgu,
        }, function (error, result) {
            if (!error) {
                res.json(result);
            } else {
                res.json(error);
            }
        })
    })


    app.post("/updatestore", function (req, res) {
        myModel2.updateOne({_id: req.body.id}, {
            $set:
                {
                    storename: req.body.storename,
                    storeaddress: req.body.storeaddress,
                    storeNumber: req.body.storeNumber,
                    storeimg: req.body.pictures,
                }
        }, function (error, result) {
            if (!error) {
                res.json(1);
            } else {
                res.json(0);
            }
        });
    });

    app.post("/deletestore", function (req, res) {
        console.log(req.body.id)
        myModel2.deleteOne({_id: req.body.id}, function (error, result) {
            if (!error) {
                res.json(result);
            } else {
                res.json(error);
            }
        });
    });


    //食品
    const schemaObj3 = new mongoose.Schema({
        foodname: String,
        foodprice: String,
        foodnum: Number,
        foodinfo: String,
        foodimg: String,
        storeid: String
    }, {collection: "food", versionKey: false})

    const myModel3 = mongoose.model("food", schemaObj3);


    let imgu2;
    app.post("/uploadF2", function (req, res) {
        let uploadObj = new fd.IncomingForm();
        uploadObj.parse(req, function (error, fields, files) {
            let fileInfo = files.my_file;
            let file_name = fileInfo.name;
            let file_temp_path = fileInfo.path;
            let target_path = "food/" + file_name;
            let readS = fs.createReadStream(file_temp_path);

            readS.on("end", function () {
                res.json({msg: "ok", img_src: target_path});
            });
            readS.on("error", function () {
                res.json({msg: "error"});
            });
            let writeS = fs.createWriteStream(target_path);
            imgu2 = target_path;
            readS.pipe(writeS);
        });
    });


    app.get("/getFood", function (req, res) {
        myModel3.find(function (error, result) {
            if (!error) {
                res.json(result);
            } else {
                res.json(error);
            }
        })
    })

    app.post("/food", function (req, res) {
        myModel3.create({
            foodname: req.body.foodname,
            foodprice: req.body.foodprice,
            foodinfo: req.body.foodinfo,
            foodnum: req.body.foodnum,
            storeimg: imgu2,
            storeid: req.body.storeid,
        }, function (error, result) {
            if (!error) {
                res.json(result);
            } else {
                res.json(error);
            }
        })
    })


    app.post("/updatefood", function (req, res) {
        myModel3.updateOne({_id: req.body.id}, {$set:
                {
                    foodname: req.body.foodname,
                    foodprice: req.body.foodprice,
                    foodinfo: req.body.foodinfo,
                    storeimg: req.body.pictures,
                }
        }, function (error, result) {
            if (!error) {
                res.json(1);
            } else {
                res.json(0);
            }
        });
    });

    app.post("/deletefood", function (req, res) {
        myModel3.deleteOne({_id: req.body.id}, function (error, result) {
            if (!error) {
                res.json(result);
            } else {
                res.json(error);
            }
        });
    });
})
